package com.skp.shaphan.ui.buttons;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;

import com.skp.shaphan.ui.dialogs.ConnectionDialog;

public class ConnectionOKButton extends JButton implements ActionListener, AncestorListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 5881384355538806782L;
	private ConnectionDialog dialog;
	private Runnable okAction;
	
	public ConnectionOKButton(ConnectionDialog dialog) {
		super("Ok");
		this.okAction = null;
		this.dialog = dialog;
		this.addActionListener(this);
		this.addAncestorListener(this);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		okAction.run();
		dialog.dispose();
	}

	@Override
	public void ancestorAdded(AncestorEvent event) {
		this.getRootPane().setDefaultButton(this);
	}

	@Override
	public void ancestorMoved(AncestorEvent event) {
	}

	@Override
	public void ancestorRemoved(AncestorEvent event) {
	}

	public void setOKAction(Runnable okAction) {
		this.okAction = okAction;
	}
	public Runnable getOKAction() {
		return this.okAction;
	}
}
